"""
应用扩展
定义和配置应用所需的扩展，如数据库、JWT、Marshmallow等
"""
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_jwt_extended import JWTManager
from flask_marshmallow import Marshmallow
from flask_cors import CORS
from flask_socketio import SocketIO
from flask_caching import Cache
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

# 数据库
db = SQLAlchemy()

# 数据库迁移
migrate = Migrate()

# JWT认证
jwt = JWTManager()

# Marshmallow
ma = Marshmallow()

# CORS
cors = CORS()

# Socket.IO
socketio = SocketIO()

# 缓存
cache = Cache()

# 请求限速
limiter = Limiter(
    key_func=get_remote_address,
    default_limits=["200 per day", "50 per hour"]
)

def init_extensions(app):
    """初始化应用扩展"""
    db.init_app(app)
    migrate.init_app(app, db)
    jwt.init_app(app)
    ma.init_app(app)
    cors.init_app(app)
    socketio.init_app(app, cors_allowed_origins="*") 